package com.alliky.core.base;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.multidex.MultiDexApplication;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alliky.core.BuildConfig;
import com.alliky.core.R;
import com.alliky.core.api.BaseApi;
import com.alliky.core.config.Config;
import com.alliky.core.crash.CrashConfig;
import com.alliky.core.net.interceptor.DebugInterceptor;
import com.alliky.core.util.KLog;
import com.alliky.core.util.Utils;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.tamsiree.rxkit.RxTool;

import me.jessyan.retrofiturlmanager.RetrofitUrlManager;

/**
 * @Description TODO
 * @Author wxianing
 * @Date 2020-11-14
 */
public class BaseApplication extends MultiDexApplication {

    private static Application mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        setApplication(this);
        //初始化常用配置
        Config.init(this)
                .withLoaderDelayed(500)
                .withApiHost(BaseApi.BASE_URL)
                .withInterceptor(new DebugInterceptor("test", com.alliky.core.R.raw.test))
                .configure();

        //配置全局异常崩溃操作
        CrashConfig.Builder.create()
                .backgroundMode(CrashConfig.BACKGROUND_MODE_SILENT) //背景模式,开启沉浸式
                .enabled(true) //是否启动全局异常捕获
                .showErrorDetails(true) //是否显示错误详细信息
                .showRestartButton(true) //是否显示重启按钮
                .trackActivities(true) //是否跟踪Activity
                .minTimeBetweenCrashesMs(2000) //崩溃的间隔时间(毫秒)
                .errorDrawable(R.mipmap.icon_logo) //错误图标
//                .restartActivity(LoginActivity.class) //重新启动后的activity
                //.errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
                //.eventListener(new YourCustomEventListener()) //崩溃后的错误监听
                .apply();
        //初始化SmartRefreshLayout刷新头部与尾部样式
        SmartRefreshLayout.setDefaultRefreshInitializer((context, layout) -> {
            layout.setRefreshHeader(new WaterDropHeader(context));
            layout.setRefreshFooter(new ClassicsFooter(context));
        });

        RetrofitUrlManager.getInstance().setGlobalDomain(BaseApi.BASE_URL2);
    }


    /**
     * 当主工程没有继承BaseApplication时，可以使用setApplication方法初始化BaseApplication
     *
     * @param application
     */
    public static synchronized void setApplication(@NonNull Application application) {
        mInstance = application;
        ARouter.init(application);
        //初始化工具类
        Utils.init(application);
        RxTool.init(application);
        KLog.init(BuildConfig.DEBUG);

        //注册监听每个activity的生命周期,便于堆栈式管理
        application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                KLog.d("onActivityCreated>>>", activity.getClass().getSimpleName());
                AppManager.getInstance().addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                KLog.d("onActivityDestroyed>>>", activity.getClass().getSimpleName());
                AppManager.getInstance().removeActivity(activity);
            }
        });
    }

    /**
     * 获得当前app运行的Application
     */
    public static Application getInstance() {
        if (mInstance == null) {
            throw new NullPointerException("Please inherit BasicApplication or call setApplication.");
        }
        return mInstance;
    }
}
